package com.wjerp.tplus.server.api.controller;


import com.wjerp.tplus.server.api.controller.base.BaseController;
import com.wjerp.tplus.server.api.request.BaseRequestPage;
import com.wjerp.tplus.server.api.request.QueryCompanyListRequest;
import com.wjerp.tplus.server.api.request.UpdateCompanyInfoRequest;
import com.wjerp.tplus.server.api.response.CompanyInfoResponse;
import com.wjerp.tplus.server.api.response.PageDataResponse;
import com.wjerp.tplus.server.common.exception.BusinessException;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import com.wjerp.tplus.server.common.security.JwtUser;
import com.wjerp.tplus.server.domain.entity.CompanyAccountChangeInfo;
import com.wjerp.tplus.server.domain.entity.CompanyAccountInfo;
import com.wjerp.tplus.server.domain.service.CompanyAccountChangeInfoService;
import com.wjerp.tplus.server.domain.service.CompanyService;
import com.wjerp.tplus.server.domain.service.CompanyAccountInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lishuailei
 */
@Api(tags = "系统管理—- 公司管理",description = "系统管理——公司管理相关API")
@RestController
@RequestMapping("sys/company")
public class CompanyController extends BaseController {

    @Resource
    private CompanyService companyService;

    @Resource
    private CompanyAccountInfoService companyaccountInfoService;

    @Resource
    private CompanyAccountChangeInfoService companyAccountChangeInfoService;

    @ApiOperation("公司信息查询（分页）")
    @PostMapping("loadCompanyList")
    public ResponseEntity<BaseResponseData> loadCompanyList(@RequestBody QueryCompanyListRequest request){
        PageDataResponse<CompanyInfoResponse> pageDataResponse = companyService.loadCompanyList(request);
        return success(pageDataResponse);
    }

    @ApiOperation("公司信息查询")
    @GetMapping("loadCompanyInfo/{companyCode}")
    public ResponseEntity<BaseResponseData> loadCompanyInfo(@PathVariable("companyCode") String companyCode){
        CompanyInfoResponse companyInfo = companyService.loadCompanyInfoByCompanyCode(companyCode);
        return success(companyInfo);
    }

    @ApiOperation("新增公司信息")
    @PostMapping("addCompany")
    public ResponseEntity<BaseResponseData> addCompany(@RequestBody UpdateCompanyInfoRequest request){
       return editCompany(null,request);
    }

    @ApiOperation("编辑公司信息")
    @PutMapping("editCompany/{companyCode}")
    public ResponseEntity<BaseResponseData> editCompany(
            @PathVariable("companyCode") String companyCode,
            @RequestBody UpdateCompanyInfoRequest request){
        try {
            checkParam(request);
            String userDisplayId = getUserDisplayId();
            request.setCompanyCode(companyCode);
            companyService.addOrUpdateCompanyInfo(userDisplayId,request);
            return success(true);
        } catch (BusinessException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        } catch (ServiceException e) {
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        }

    }

    @ApiOperation("公司账户信息查询")
    @GetMapping("loadCompanyAccountInfo/{companyCode}")
    public ResponseEntity<BaseResponseData> loadCompanyAccountInfo(@PathVariable("companyCode") String companyCode){
        CompanyAccountInfo accountInfo = companyaccountInfoService.loadCompanyAccountInfoByCompanyCode(companyCode);
        return success(accountInfo);
    }

    @ApiOperation("公司账户变更记录信息查询")
    @PutMapping("loadCompanyAccountChangeInfo/{companyCode}")
    public ResponseEntity<BaseResponseData> loadCompanyAccountChangeInfo(@PathVariable("companyCode") String companyCode,
                                                                         @RequestBody BaseRequestPage request){
        PageDataResponse<CompanyAccountChangeInfo> pageDataResponse = companyAccountChangeInfoService.loadCompanyAccountChangeInfoByCompanyCode(companyCode,request);
        return success(pageDataResponse);
    }

    @ApiOperation("公司账户刷新")
    @PutMapping("refreshAccount")
    public ResponseEntity<BaseResponseData> refreshAccount(){
        JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
        try {
            companyaccountInfoService.refreshAccount(currentLoginUserInfo.getCompanyCode(),currentLoginUserInfo.getDisplayUserId());
            return success(true);
        } catch (ServiceException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        }
    }


    /**
     * 检查参数信息
     * @param request
     * @throws ServiceException
     */
    private void checkParam(UpdateCompanyInfoRequest request) throws ServiceException {

        if(StringUtils.isEmpty(request.getName())){
            throw new ServiceException("请输入企业名称");
        }
        if(StringUtils.isEmpty(request.getTaxPayerCode())){
            throw new ServiceException("请输入企业税号");
        }

        if(StringUtils.isEmpty(request.getTaxPayerAddress())){
            throw new ServiceException("请输入注册地址");
        }
    }

}
